/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.device.servlet;

import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiendpointbean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * 获取设备列表
 *
 * @author LiuShang
 */
@WebServlet(name = "cn.net.greenet.device.servlet.ListService", urlPatterns = {"/device/list"})
public class List extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String type = request.getParameter("type");
        PrintWriter out = response.getWriter();
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        try {
            Dpiendpointbean bean = null;
            java.util.List l = null;
            if ("eu".equals(type)) {
                l = session.createQuery("FROM Dpiendpointbean  WHERE probeType =1").list();
            } else if ("dpi".equals(type)) {
                l = session.createQuery("FROM Dpiendpointbean  WHERE probeType =0").list();
            } else if ("all".equals(type)) {
                l = session.createQuery("FROM Dpiendpointbean").list();
            }

            JSONArray jSONArray = new JSONArray();
            for (int i = 0; i < l.size(); i++) {
                bean = (Dpiendpointbean) l.get(i);
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("name", bean.getName());
                jSONObject.put("ip", bean.getMasterIp());
                jSONObject.put("id", bean.getId() + "");
                jSONObject.put("isCCOn", bean.isIsClientChannelConnected() ? "1" : "0");
                jSONObject.put("isSCOn", bean.isIsServerChannelConnected() ? "1" : "0");
                jSONObject.put("isEnabled", bean.isEnabled() ? "1" : "0");
                jSONArray.add(jSONObject);
            }

            try {
                /* TODO output your page here. You may use following sample code. */
                out.println(jSONArray);
            } catch (Exception e) {
                out.println("[]");
            }

        } catch (Exception e) {
            out.println("[]");
        } finally {
            out.close();
            session.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
